<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app;

abstract class AgentRest extends BaseController
{
    public $_app;
    public $_controller;
    public $_action;
    public $_method = "GET";
    public $_param = [];
    public $_input = [];
    public $_header = [];
    public $_token;
    public $_lang = "zh-cn";
    public $_role = "guest";
    public $_host;
    public $_ip;
    public $_user;
    public $_user_id;
    public $_uniacid = 666;
    protected $_config = [];
    protected $autograph = "";
    protected $uniacid = 0;
    protected $is_app = 0;
    protected $defaultImage = ["avatar" => "https://lbqnyv2.migugu.com/defaultAvatar.png", "image" => "https://lbqnyv2.migugu.com/lbCardDefaultImage.png"];
    protected $check_url = "https://longyiyi.migugu.com/wexinPay.php?ck=7899";
    protected $noNeedLogin = [];
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        if(in_array($this->_method, ["options", "Options", "OPTIONS"])) {
            echo true;
            exit;
        }
        $this->_param = $this->request->param();
        $this->_input = json_decode($this->request->getInput(), true);
        $this->_header = $this->request->header();
        if(defined("LONGBING_CARD_UNIACID")) {
            define("LONGBING_CARD_UNIACID", $this->_uniacid);
        }
        $this->is_app = !empty($this->_header["isapp"]) ? $this->_header["isapp"] : 0;
        $this->shareChangeData($this->_param);
        if(!$this->match($this->noNeedLogin)) {
        }
        $this->_config = longbingGetAppConfig($this->_uniacid);
        if(isset($this->_header["lang"])) {
            $this->_token = $this->_header["lang"];
        }
        if(!empty($this->autograph) && !$this->match($this->noNeedLogin)) {
            $this->_user_id = $this->getUserId();
            $this->_user = $this->getUserInfo();
        }
    }
    public function shareChangeData($input)
    {
        $arr = ["shop/IndexShopGoods/goodsInfo", "im/app/getCustomerUnReadMessageCount", "decorate/IndexCase/caseInfo"];
        if(!empty($input["timeline"]) && $input["timeline"] == 1 && !empty($input["s"]) && in_array($input["s"], $arr)) {
            $input["s"] = trim(strrchr($input["s"], "/"), "/");
            $this->noNeedLogin[] = $input["s"];
        }
        return true;
    }
    public function match($arr)
    {
        $arr = is_array($arr) ? $arr : explode(",", $arr);
        if(!$arr) {
            return false;
        }
        $arr = array_map("strtolower", $arr);
        if(in_array(strtolower($this->request->action()), $arr) || in_array("*", $arr)) {
            return true;
        }
        return false;
    }
    public function _empty($method)
    {
        if(method_exists($this, $method . "_" . $this->method . "_" . $this->type)) {
            $fun = $method . "_" . $this->method . "_" . $this->type;
        } elseif($this->method == $this->restDefaultMethod && method_exists($this, $method . "_" . $this->type)) {
            $fun = $method . "_" . $this->type;
        } elseif($this->type == $this->restDefaultType && method_exists($this, $method . "_" . $this->method)) {
            $fun = $method . "_" . $this->method;
        }
        if(isset($fun)) {
            return \think\App::invokeMethod([$this, $fun]);
        }
        throw new \Exception("error action :" . $method);
    }
    protected function getUserInfo()
    {
        $value = getCache($this->autograph, $this->_uniacid);
        if(empty($value)) {
            $this->errorMsg("need login", 401);
        }
        $user_model = new massage\model\User();
        $value["balance"] = $user_model->where(["id" => $value["id"]])->value("balance");
        if($this->is_app == 1) {
            $value["openid"] = $value["app_openid"];
        } elseif($this->is_app == 0) {
            $value["openid"] = $value["wechat_openid"];
        } else {
            $value["openid"] = $value["web_openid"];
        }
        return $value;
    }
    public function getCapInfo($err = 1)
    {
        $user_id = $this->getUserId();
        $user_model = new shop\model\User();
        $cap_id = $user_model->where(["id" => $user_id])->value("cap_id");
        $cap_info = [];
        if(!empty($cap_id)) {
            $cap_model = new shop\model\Cap();
            $cap_info = $cap_model->dataInfo(["id" => $cap_id, "status" => 2]);
        }
        if(empty($cap_info) && $err == 1) {
            $this->errorMsg("请选择楼长", -407);
        }
        return $cap_info;
    }
    protected function getUserId()
    {
        $value = getCache($this->autograph, $this->_uniacid);
        if($value === false && !$this->match($this->noNeedLogin)) {
            $this->errorMsg("need login", 401);
        }
        return $value["id"];
    }
    public function payConfig($uniacid = "", $is_app = 7)
    {
        if($is_app == 7) {
            $is_app = $this->is_app;
        }
        $uniacid_id = !empty($uniacid) ? $uniacid : $this->_uniacid;
        $pay = \think\facade\Db::name("shequshop_school_pay_config")->where(["uniacid" => $uniacid_id])->find();
        $config = \think\facade\Db::name("shequshop_school_config")->where(["uniacid" => $uniacid_id])->find();
        if(empty($pay["mch_id"]) || empty($pay["pay_key"])) {
            $this->errorMsg("未配置支付信息" . $uniacid_id);
        }
        $setting["payment"]["merchant_id"] = $pay["mch_id"];
        $setting["payment"]["key"] = $pay["pay_key"];
        $setting["payment"]["cert_path"] = $pay["cert_path"];
        $setting["payment"]["key_path"] = $pay["key_path"];
        $setting["payment"]["ali_appid"] = $pay["ali_appid"];
        $setting["payment"]["ali_privatekey"] = $pay["ali_privatekey"];
        $setting["payment"]["ali_publickey"] = $pay["ali_publickey"];
        $setting["company_pay"] = $config["company_pay"];
        if($is_app == 0) {
            $setting["app_id"] = $config["appid"];
            $setting["secret"] = $config["appsecret"];
        } elseif($is_app == 1) {
            $setting["app_id"] = $config["app_app_id"];
            $setting["secret"] = $config["app_app_secret"];
        } else {
            $setting["app_id"] = $config["web_app_id"];
            $setting["secret"] = $config["web_app_secret"];
        }
        $setting["is_app"] = $is_app;
        return $setting;
    }
    protected function getTimeData($start_time, $end_time, $coach_id, $time_, $is_coach = 0)
    {
        $time_ = (int) $time_;
        $config_model = new massage\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $end_time = strtotime($end_time) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $time_));
        $start_time = strtotime($start_time) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $time_));
        $coach_model = new massage\model\Coach();
        $coach = $coach_model->dataInfo(["id" => $coach_id]);
        $rest_arr = $coach_model->getCoachRestTime($coach, $start_time, $end_time, $config);
        $end_time = $start_time < $end_time ? $end_time : $end_time + 86400;
        $i = 0;
        $data = [];
        $time = $start_time;
        $is_eve = $end_time == $start_time ? 1 : 0;
        $time_interval = $is_coach == 1 ? 0 : $config["time_interval"];
        while ($time < $end_time) {
            $time = $start_time + $config["time_unit"] * $i * 60;
            $times = date("Y-m-d", $time) == date("Y-m-d", $time_) ? $time : $time - 86400;
            if($end_time < $time) {
                break;
            }
            if(!empty($data[0]) && $times == $data[0]["time_str"]) {
            } else {
                $data[$i]["time_str"] = $times;
                $data[$i]["time_text"] = date("H:i", $times);
                $data[$i]["time_texts"] = date("Y-m-d", $times);
                $data[$i]["is_click"] = 0;
                $data[$i]["is_order"] = 0;
                $data[$i]["status"] = 1;
                $where = [];
                $where[] = ["coach_id", "=", $coach_id];
                $where[] = ["start_time", "<=", $times + $time_interval];
                $where[] = ["end_time", ">=", $times - $time_interval];
                $where[] = ["pay_type", "not in", [-1]];
                $order = \think\facade\Db::name("massage_service_order_list")->where($where)->field("start_time")->find();
                if(!empty($order)) {
                    $data[$i]["is_order"] = 1;
                    $data[$i]["status"] = 0;
                }
                if(!empty($rest_arr) && $data[$i]["status"] == 1) {
                    foreach ($rest_arr as $values) {
                        if($times == $values["time_str"]) {
                            $data[$i]["status"] = 0;
                        }
                    }
                }
                $t = time();
                $data[$i]["status"] = $times < $t ? 0 : $data[$i]["status"];
                if(empty($is_eve)) {
                    $data[$i]["status"] = $time == $end_time ? 0 : $data[$i]["status"];
                }
                $i++;
            }
        }
        $data = !empty($data) ? arraySort($data, "time_str") : $data;
        return $data;
    }
    protected function getTimeDataV2($start_time, $end_time, $coach_id, $time_)
    {
        $time_ = (int) $time_;
        $config_model = new massage\model\Config();
        $config = $config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $where = ["start_time" => $start_time, "end_time" => $end_time, "coach_id" => $coach_id, "date" => date("Y-m-d", $time_), "max_day" => $config["max_day"], "time_unit" => $config["time_unit"]];
        $info = \think\facade\Db::name("massage_service_coach_time")->where($where)->find();
        if($info) {
            $info = json_decode($info["info"], true);
            foreach ($info as &$value) {
                $where = [];
                $where[] = ["coach_id", "=", $coach_id];
                $where[] = ["start_time", "<=", $value["time_str"]];
                $where[] = ["end_time", ">=", $value["time_str"]];
                $where[] = ["pay_type", "not in", [-1]];
                $order = \think\facade\Db::name("massage_service_order_list")->where($where)->find();
                if(!empty($order)) {
                    $value["status"] = 0;
                    $value["is_order"] = 1;
                } else {
                    $value["is_order"] = 0;
                }
                $value["status"] = $value["time_str"] < time() ? 0 : $value["status"];
            }
            return $info;
        } else {
            $end_time = strtotime($end_time) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $time_));
            $start_time = strtotime($start_time) - strtotime(date("Y-m-d", time())) + strtotime(date("Y-m-d", $time_));
            $end_time = $start_time < $end_time ? $end_time : $end_time + 86400;
            $i = 0;
            $data = [];
            $time = $start_time;
            while ($time < $end_time) {
                $time = $start_time + $config["time_unit"] * $i * 60;
                $times = date("Y-m-d", $time) == date("Y-m-d", $time_) ? $time : $time - 86400;
                if($end_time < $time) {
                    break;
                }
                if(!empty($data[0]) && $times == $data[0]["time_str"]) {
                } else {
                    $data[$i]["time_str"] = $times;
                    $data[$i]["time_text"] = date("H:i", $times);
                    $data[$i]["time_texts"] = date("Y-m-d", $times);
                    $data[$i]["is_click"] = 0;
                    $where = [];
                    $where[] = ["coach_id", "=", $coach_id];
                    $where[] = ["start_time", "<=", $times];
                    $where[] = ["end_time", ">=", $times];
                    $where[] = ["pay_type", "not in", [-1]];
                    $order = \think\facade\Db::name("massage_service_order_list")->where($where)->find();
                    if(!empty($order)) {
                        $data[$i]["is_order"] = 1;
                        $data[$i]["status"] = 0;
                    } else {
                        $data[$i]["is_order"] = 0;
                        $data[$i]["status"] = 1;
                    }
                    $t = "1666281540";
                    $data[$i]["status"] = $times < $t ? 0 : $data[$i]["status"];
                    $i++;
                }
            }
            $data = !empty($data) ? arraySort($data, "time_str") : $data;
            return $data;
        }
    }
}

?>